package com.xuecheng.media.controller;


import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.media.model.dto.QueryMediaParamsDto;
import com.xuecheng.media.model.dto.UploadFileParamsDto;
import com.xuecheng.media.model.dto.UploadFileResultDto;
import com.xuecheng.media.model.pojo.MediaFiles;
import com.xuecheng.media.service.MediaFilesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;


/**
 * 媒资文件管理接口
 *
 * @author xn
 * @date 2023/05/24
 */
@Tag(name = "媒资文件管理接口")
@RestController
public class MediaFilesController {

    @Resource
    MediaFilesService mediaFilesService;

    @Operation(summary = "媒资列表查询接口")
    @PostMapping("/files")
    public PageResult<MediaFiles> list(PageParams pageParams, @RequestBody QueryMediaParamsDto queryMediaParamsDto){
        Long companyId = 1232141425L;
        return mediaFilesService.queryMediaFiels(companyId,pageParams,queryMediaParamsDto);

    }
    @Operation(summary = "上传图片")
    @RequestMapping(value = "upload/coursefile",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public UploadFileResultDto upload(@RequestPart("filedata") MultipartFile file) throws IOException {
        Long companyId = 1232141425L;

        UploadFileParamsDto uploadFileParamsDto = new UploadFileParamsDto();

        //文件名
        uploadFileParamsDto.setFilename(file.getOriginalFilename());

        //文件大小
        uploadFileParamsDto.setFileSize(file.getSize());

        //文件类型
        uploadFileParamsDto.setFileType("001001");

        //创建临时文件
        File tempFile = File.createTempFile("minio", ".temp");

        file.transferTo(tempFile);

        //文件路径
        String absolutePath = tempFile.getAbsolutePath();

        mediaFilesService.uploadFile(companyId,uploadFileParamsDto,absolutePath);
        return null;
    }

}
